<script setup>

import PageContainer from "@/components/PageContainer.vue";
import {ref} from "vue";
import {Delete, Edit} from "@element-plus/icons-vue";
import ChannelSelect from "@/views/article/components/ChannelSelect.vue";
import {artDelService, artGetListService} from "@/api/article.js";
import {formatTime} from "@/utils/format.js";
import ArticleEdit from "@/views/article/components/ArticleEdit.vue";
import {ElMessage, ElMessageBox} from "element-plus";

const articleList = ref([])
const articleEditRef = ref()
const total = ref(0)
const loading = ref(false)


const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})


const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  console.log(res)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getArticleList()

const onSizeChange = (size) => {
  params.value.pagenum = 1
  params.value.pagesize = size
  getArticleList()
}
const onCurrentChange = (page) => {
  params.value.pagenum = page
  getArticleList()
}
const onSearch = () => {
  params.value.pagenum = 1
  getArticleList()
}
const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}

const onAddArticle = () => {
  articleEditRef.value.open({})
}

const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}

const onSuccess = (type) => {
  if (type === 'add') {
    var lastPage = Math.ceil((total.value + 1) / params.value.pagesize);
    params.value.pagenum = lastPage
  }
  getArticleList()
}

const onDeleteArticle = async (row) => {
  await ElMessageBox.confirm('你确定要删除这条信息么?', "温馨提示", {
    type: 'waring',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelService(row.id)
  ElMessage({type: 'success', message: '删除成功'})
  getArticleList()
}


</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">发布文章</el-button>
    </template>
    <el-form inline>
      <el-form-item label="文章分类：">
        <channel-select></channel-select>
      </el-form-item>
      <el-form-item label="发文状态">
        <el-select v-model="params.cate_id">
          <el-option label="已发布" value="发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="onSearch" type="primary">
          搜索
        </el-button>
        <el-button @click="onReset">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="articleList" style="width: 100%">
      <el-table-column label="文章标题" width="400">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间">
        <template #default="{row}">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{row}">
          <el-button
              :icon="Edit"
              circle
              plain
              type="primary"
              @click="onEditArticle(row)"
          ></el-button>
          <el-button
              :icon="Delete"
              circle
              plain
              type="danger"
              @click="onDeleteArticle(row)">
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        v-model:current-page="params.pagenum"
        v-model:page-size="params.pagesize"
        :page-sizes="[2,3,4,5,10]"
        layout="jumper, total, sizes, prev, pager, next"
        background
        :total="total"
        @size-change="onSizeChange"
        @current-change="onCurrentChange"
        style="margin-top: 20px;justify-content:  flex-end">

    </el-pagination>
    <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
  </page-container>
</template>
<style lang="scss" scoped>

</style>
